package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.jgspatrol.bean.TemporaryTaskBean;
import com.mingtu.jgspatrol.databinding.ItemTaskCarryOutBinding;
import com.mingtu.jgspatrol.holder.BaseViewBindingHolder;

import java.util.ArrayList;
import java.util.List;

public class TaskCarryOut2Adapter extends RecyclerView.Adapter<BaseViewBindingHolder> implements View.OnClickListener {

    private Context context;
    private List<TemporaryTaskBean.DataBean.AssignmentUserEntityListBean> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;


    public TaskCarryOut2Adapter(Context context, List<TemporaryTaskBean.DataBean.AssignmentUserEntityListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public BaseViewBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_carry_out, parent, false);
        ItemTaskCarryOutBinding binding = ItemTaskCarryOutBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        // 实例化viewholder
        BaseViewBindingHolder viewHolder = new BaseViewBindingHolder(binding);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewBindingHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                ItemTaskCarryOutBinding binding = (ItemTaskCarryOutBinding) holder.getBinding();
                TemporaryTaskBean.DataBean.AssignmentUserEntityListBean finishBean = mData.get(position);
                String name = finishBean.getName();
                String completeTime = finishBean.getCompleteTime();
                if (!StringUtils.isEmpty(name)) {
                    binding.tvName.setText(name + "");
                }
                if (!StringUtils.isEmpty(completeTime)) {
                    binding.tvTime.setText("完成时间：" + completeTime);
                } else {
                    binding.tvTime.setText("未完成");
                }
            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }


    public void upData(List<TemporaryTaskBean.DataBean.AssignmentUserEntityListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
